<?php
namespace Admin\Model;

use Admin\Model\BaseModel;
use Admin\Controller\PublicController;

class AskTagsModel extends BaseModel{
    protected $_validate = array(
        //array(验证字段1,验证规则,错误提示,[验证条件,附加规则,验证时间]),
    	array('ask_tags_name', 'require', '请输入标签', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
    	array('ask_tags_name', 'checkName', '标签名称已经存在', self::MUST_VALIDATE, 'callback', self::MODEL_BOTH),
    	array('ask_tags_name', '1,10', '标签不能超过20个字符', self::MUST_VALIDATE, 'length', self::MODEL_BOTH),
    	array('ask_category_id', 'checkId', '请选择分类', self::MUST_VALIDATE, 'callback', self::MODEL_BOTH),
    );
    
    /**
     * @todo 判断值
     * @param  string $id 
     * @return boolean     ture - id成立，false - id无效
     */
    protected function checkId($id){
    	if($id == 0 || $id == ''){
    		return false;
    	}else{
    		return true;
    	}
    }
    
    /**
     * @todo 检测名称是否存在
     * @param  string   $ask_tags_name 文章标签名称
     * @return boolean     ture - 未重名，false - 已重名
     * @author Cruise <jiangyu@hrbjjhh.com>
     */
    protected function checkName($ask_tags_name){
    	$ask_tags_id = (int)I('ask_tags_id',0);
    	if($ask_tags_id > 0){
    		$map['ask_tags_id'] = array('neq',$ask_tags_id);
    	}
    	$map['ask_tags_name'] = $ask_tags_name;
    	$map['status'] = array('gt',-1);
    	if ($this->where($map)->field('ask_tags_id')->find()){ 
    		return false;
    	}
    	return true;
    }
    
    /**
     * 新增或更新一个标签之后清除表中标签分类关系
     * 并写入新的对应关系
     * @author cruise <jiangyu@hrbjjhh.com>
     */
    public function changeTagCategory($ask_tags_id = 0,$ask_category_ids){
    	$ask_tags_id = (int)$ask_tags_id;
    	$ask_category_id = $ask_category_ids;
    	if(empty($ask_tags_id)){
    		$this->error = '参数错误';
    	}
    	$object = M('askCategoryTags');
    	$map['ask_tags_id'] = $ask_tags_id;
    	$check = $object->where($map)->select();
    	if($check != 0){
    		$delete = $object->where($map)->delete();
    		if($delete == 0){
    			$this->error = '数据清除失败';
    		}
    	}
    	$date_final = array();
    	foreach ($ask_category_id as $val){
    		$date_final[] = array(
    				'ask_category_id' => $val,
    				'ask_tags_id' => $ask_tags_id
    		);
    	}
    	$final = $object->addAll($date_final);
    	if(!$final){
    		return false;
    	}
    	return true;
    }
    
    /**
     * @todo 删除关系表
     * @author Cruise <jiangyu@hrbjjhh.com>
     */
    Public function delectTagsCategory($id){
    	$id = (int)$id;
    	if(empty($id)) {
    		return false;
    	}
    	$map['ask_tags_id'] = array('in',$id);
    	$map['status'] = array('gt',-1);
    	$category_delect = M('askCategoryTags')->where($map)->delete();
    	if (!$category_delect){
    		return false;
    	}
    	return true;
    }
    
    /**
     * @todo 调取二级分类接口
     * @author Cruise <jiangyu@hrbjjhh.com>
     */
    public function getAskCategoryId($ask_category_id){
    	$map['ask_category_pid'] = $ask_category_id;
    	$map['status'] = 1;
    	return  M('AskCategory')->where($map)->order('ask_category_id desc')->field('ask_category_id,ask_category_name')->select();
    }
    
    /**
     * update方法
     * @author Cruise <jiangyu@hrbjjhh.com>
     */
    public function update(){
         
         /* 获取数据对象 */
         $_POST['position'] = $this->getPosition();
         $data = $this->create($_POST);
    
         if(empty($data)){
             $this->error =$this->getError();
             return false;
         }
         /* 添加或新增活动配置 */
         if(empty($data[$this->getPk()])){ //新增数据
             $id = $this->add(); //添加活动配置
             if(!$id){
                 $this->error = '新增出错！';
                return false;
             }
         } else { //更新数据
             $id = $this->save(); //更新活动配置
            if(false === $id){
                 $this->error = '更新出错！';
                 return false;
             }
         }
         return $id;
    }
    
    /**
     * 生成推荐位的值
     * @return number 推荐位
     * @author Cruise <jiangyu@hrbjjhh.com>
     */
    protected function getPosition(){
        $position = I('post.position');
        if(!is_array($position)){
            return 0;
        }else{
            $pos = 0;
            foreach ($position as $key=>$value){
                $pos += $value;		//将各个推荐位的值相加
            }
            return $pos;
        }
    }
    
    /**
     * 设置where查询条件
     * @param  number  $pos      推荐位 
     * @return array             查询条件
     * @author Cruise <jiangyu@hrbjjhh.com>
     */
    public function listMap( $pos = null){
        /* 设置推荐位 */
        if(is_numeric($pos)){
            $map[] = "position & {$pos} = {$pos}";
        }else{
        	$map = array();
        }
        return $map;
    }
    
    /**
     * 编辑标签数据处理
     * @param  int     $id       标签id
     * @author Cruise <jiangyu@hrbjjhh.com>
     */
    public function editAskTagsInfo($id = 0){
    	$id = (int)$id;
    	if(0 == $id){
    		$this->error = '参数错误';
    		return false;
    	}
    	$obj_api = D('ServiceApi');
    	$tag_info = $obj_api->getAskTagsInfo($id);
    	if(empty($tag_info)){
    		$this->error = '标签信息不存在';
    		return false;
    	}
    	$tag_category = M('AskCategoryTags')->where(array('ask_tags_id'=>$tag_info['ask_tags_id']))->field('ask_category_id')->find();
    	if(!empty($tag_category)){
    		$tag_info['ask_category_id'] = $tag_category['ask_category_id'];
    	}
    	return $tag_info;
    }
    
    /**
     * @todo 返回最后一次错误信息
     * @return string
     * @author Cruise <jiangyu@hrbjjhh.com>
     */
    public function getError(){
    	return $this->error;
    }
    
    /**
     * @todo 返回主键
     * @see \Think\Model::getPk()
     */
    public function getPk(){
    	return 'ask_tags_id';
    }
}